<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Faq extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'faq';
        $this->load->model('Mfaq');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 7, $this->session->userdata('admin_id'));
    }
    
    public function index()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['data'] = $this->Mfaq->indexDb();
        
        $this->data['action'] = 'index';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addFaq()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('question', 'Câu hỏi', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $this->data['data'] = $this->Mfaq->addFaqDb($this->getParamString('question'),$this->getParamString('answer'));
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/info/add-faq";</script>');
            }
        }
        
        $this->data['action'] = 'addfaq';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editFaq()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('question', 'Câu hỏi', 'required');
        
        $id = $this->getParamUri(4,1);
        $this->data['data'] = $this->Mfaq->getFaqDb($id);
        if($this->form_validation->run() === TRUE)
        {
            $this->data['data'] = $this->Mfaq->editFaqDb($id,$this->getParamString('question'),$this->getParamString('answer'));
            if($this->data['data'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/info/edit-faq/'.$id.'";</script>');
            }
        }
        
        $this->data['action'] = 'editfaq';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deleteFaq()
    {
        if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $id = $this->getParamInt('id');
        die(json_encode($this->Mfaq->deleteFaqDb($id)));
    } 
}